/* eslint-disable no-unused-vars */
import React from 'react'
import {
  IRON_TIER,
  BRONZE_TIER,
  SILVER_TIER,
  GOLD_TIER,
  PLATINUM_TIER,
  EMERALD_TIER,
  DIAMOND_TIER,
  MASTER_TIER,
  GRANDMASTER_TIER,
  CHALLENGER_TIER
} from '../../../static'

// 使用对象字面量
export const KDA_COLOR = {
  '汗血宝马': 'blue',
  '上等马': 'steelblue',
  '中等马': 'yellow',
  '下等马': 'orange',
  '牛马': 'red',
  '马粪': 'grey',
  '没马': 'black',
};

export const GET_TAG_COLOR = (kdaType: keyof typeof KDA_COLOR) => KDA_COLOR[kdaType] || ''

/**
 * 日期格式转换函数
 * 例如2020-06-27T14:20:27.000000Z 时间格式转换成 2020-06-27 14:20:27
 * @param {timestamp} 原格式日期
 */

export const TIMES_TO_DATE = (timestamp: number): string => {
  const date = new Date(timestamp)
  const year = String(date.getFullYear())
  const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  return `${year}-` + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + `-${day}`
}

/**
 * 筛选自己的队伍
 * @param res
 * @returns
 */
export const FILTER_MY_TEAM = async (res: any): Promise<any> => {
  const matchedTeams = []
  if (res && res.data && res.data.games && res.data.games.games) {
    for (let x = 0; x < res.data.games.games.length; x++) {
      const element = res.data.games.games[x]
      const teamId = element.participants[0].teamId
      for (let j = 0; j < element.teams.length; j++) {
        const _j = element.teams[j]
        if (teamId === _j.teamId) {
          matchedTeams.push(_j)
        }
      }
    }
  }
  return matchedTeams
}

/**
 * 再根据我找出我的队伍stats
 * @param allTeam
 * @param me
 * @returns
 */
export const SPLIT_ALL_TEAM = async (allTeam: any[], me: any): Promise<any> => {
  let result: any[] = []
  const index = allTeam.findIndex(item => item.participantId === me[0].participantId)
  if (index <= 4) {
    result = allTeam.slice(0, 5)
  } else if (index >= allTeam.length - 5) {
    result = allTeam.slice(-5)
  }
  return result
}

/**
 * 筛选出我方队伍使用的英雄列表
 * @param {meTeam, oneTeam, twoTeam}
 * @returns
 */
export const FILTER_TEAM_DATA = ({ meTeam, oneTeam, twoTeam }: { meTeam: any; oneTeam: any[]; twoTeam: any[] }) => {
  if (meTeam.championId === oneTeam[0].id) {
    return oneTeam
  }
  return twoTeam
}

interface Item {
  participantId: number
  championId: number
  stats: {
    kills: number
    deaths: number
    assists: number
  }
}

/**
 * 返回击杀数最多的ITEM
 * @param arr
 * @returns
 */
export const FIND_MAX_KILLS = (arr: Item[]): Item => {
  let maxKillsItem: Item = arr[0]
  for (let i = 1; i < arr.length; i++) {
    if (arr[i].stats.kills > maxKillsItem.stats.kills) {
      maxKillsItem = arr[i]
    }
  }
  return maxKillsItem
}

/**
 * 我方MVP数据
 * @param participantId
 * @param list
 * @returns
 */
export const FLITER_HERO = (participantId: number, list: any): any => {
  return list.filter((item: any) => item.participantId === participantId)
}

/**
 * 获取我方MVP召唤师数据
 * @param championId
 * @param list
 * @returns
 */
export const FILTER_HERO_NAME_DATA = (championId: number, list: any) => {
  return list.filter((item: any) => item.id === championId)
}

/**
 * 获取最近一局，我使用的英雄数据
 * @param id
 * @param lastData
 * @returns
 */
export const GET_LAST_MY_HERO_NAME = (id: number, lastData: any) => {
  const filteredData = lastData.filter((item: any) => {
    if (item.id === id) {
      return item.name
    }
  })
  return filteredData
}

interface MatchesProps {
  win: string
}

export const FILTER_WIN_FAIL = async (arr: Array<object>): Promise<any> => {
  const numberMatches = arr.length
  let winNumber = 0
  arr.filter((item: any) => {
    if (item?.win === 'Win') {
      winNumber++
    }
  })
  return [winNumber, numberMatches - winNumber]
}

/**
 * MatchType转中文
 * @param {queueId}
 */
export const QUERY_GAME_TYPE = (queueId: number): string => {
  switch (queueId) {
    case 420:
      return '排位单双'
    case 430:
      return '匹配模式'
    case 440:
      return '排位组排'
    case 450:
      return '极地大乱斗'
    case 1700:
      return '斗魂竞技场'
  }
  return '人机/训练/其它'
}

export const WARINGTIP = '请注意'
export const WARINGTEXT = '初始加载为本机数据，如需查询其他数据请在搜索框根据 召唤师名称#ID 查询'

export const FILTER_KDA = (k: any) => {
  if (!Array.isArray(k)) {
    if (k?.stats?.kills >= 20 && k?.stats?.kills < 9999) {
      if (k?.stats?.deaths >= 12) {
        return '上等马'
      }
      return '汗血宝马'
    } else if (k?.stats?.kills >= 16 && k?.stats?.kills < 20) {
      if (k?.stats?.deaths <= 3) {
        return '汗血宝马'
      }
      return '上等马'
    } else if (k?.stats?.kills >= 7 && k?.stats?.kills < 16) {
      if (k?.stats?.deaths <= 2) {
        return '上等马'
      }
      return '中等马'
    } else if (k?.stats?.kills >= 4 && k?.stats?.kills < 7) {
      if (k?.stats?.deaths <= 2) {
        return '中等马'
      }
      return '下等马'
    } else if (k?.stats?.kills >= 1 && k?.stats?.kills < 4) {
      if (k?.stats?.deaths <= 3) {
        return '下等马'
      }
      return '牛马'
    } else if (k?.stats?.kills === 0) {
      if (k?.stats?.deaths >= 3 && k?.stats?.deaths <= 5) {
        return '下等马'
      } else if (k?.stats?.deaths >= 6 && k?.stats?.deaths <= 8) {
        return '马粪'
      } else if (k?.stats?.deaths >= 9) {
        return '没马'
      } else if (k?.stats?.deaths < 3) {
        return '下等马'
      }
    } else {
      return '没马'
    }
  } else {
    if (k[0].stats.kills >= 20 && k[0].stats.kills < 9999) {
      if (k[0].stats.deaths >= 12) {
        return '上等马'
      }
      return '汗血宝马'
    } else if (k[0].stats.kills >= 16 && k[0].stats.kills < 20) {
      if (k[0].stats.deaths <= 3) {
        return '汗血宝马'
      }
      return '上等马'
    } else if (k[0].stats.kills >= 7 && k[0].stats.kills < 16) {
      if (k[0].stats.deaths <= 2) {
        return '上等马'
      }
      return '中等马'
    } else if (k[0].stats.kills >= 4 && k[0].stats.kills < 7) {
      if (k[0].stats.deaths <= 2) {
        return '中等马'
      }
      return '下等马'
    } else if (k[0].stats.kills >= 1 && k[0].stats.kills < 4) {
      if (k[0].stats.deaths <= 3) {
        return '下等马'
      }
      return '牛马'
    } else if (k[0].stats.kills === 0) {
      if (k[0].stats.deaths <= 3) {
        return '下等马'
      } else if (k[0].stats.deaths >= 6) {
        return '没马'
      } else if (k[0].stats?.deaths <= 3) {
        return '下等马'
      }
      return '马粪'
    } else {
      return '没马'
    }
  }
}

// 根据段位生成段位文字字符串
export const GET_TIER_TEXT = (tier: string) => {
  let tierText = ''
  let tierColor = ''

  switch (tier) {
    case 'CHALLENGER':
      tierText = '王者'
      tierColor = '#b10311' // 紫红色
      break
    case 'GRANDMASTER':
      tierText = '宗师'
      tierColor = '#673AB7' // 深紫色
      break
    case 'MASTER':
      tierText = '大师'
      tierColor = '#BA68C8' // 浅紫色
      break
    case 'DIAMOND':
      tierText = '钻石'
      tierColor = '#2196F3' // 宝蓝色
      break
    case 'EMERALD':
      tierText = '翡翠'
      tierColor = '#4CAF50' // 翠绿色
      break
    case 'PLATINUM':
      tierText = '铂金'
      tierColor = '#03A9F4' // 浅蓝色
      break
    case 'GOLD':
      tierText = '黄金'
      tierColor = '#FFD700' // 黄金色
      break
    case 'SILVER':
      tierText = '白银'
      tierColor = '#C0C0C0' // 白银色
      break
    case 'BRONZE':
      tierText = '青铜'
      tierColor = '#B87333' // 黄灰色
      break
    case 'IRON':
      tierText = '黑铁'
      tierColor = '#43464B' // 灰黑色
      break
    default:
      tierText = '未定级🤡'
      tierColor = '#1677ff' // Default color
  }

  return { text: tierText, color: tierColor }
}

/**
 * 返回段位图标
 * @param tier 
 * @returns 
 */
export const GET_TIER = (tier: string) => {
  switch (tier) {
    case 'CHALLENGER':
      return CHALLENGER_TIER
    case 'GRANDMASTER':
      return GRANDMASTER_TIER
    case 'MASTER':
      return MASTER_TIER
    case 'DIAMOND':
      return DIAMOND_TIER
    case 'EMERALD':
      return EMERALD_TIER
    case 'PLATINUM':
      return PLATINUM_TIER
    case 'GOLD':
      return GOLD_TIER
    case 'SILVER':
      return SILVER_TIER
    case 'BRONZE':
      return BRONZE_TIER
    case 'IRON':
      return IRON_TIER
    default:
      ''
  }
}

export const FIND_TEAM = (participants: any, teams: any): any => {
  for (let i = 0; i < teams.length; i++) {
    if (participants[0].teamId === teams[i].teamId) {
      return teams[i]
    }
  }
  return null
}

export const INSERT_PIC = (param: string): string => {
  return `https://wegame.gtimg.com/g.26-r.c2d3c/helper/lol/assis/images/resources/usericon/${param}.png`
}
